(function() {
    var myApp = angular.module('myApp');

    myApp.controller("myPermissionController", function($scope, $rootScope, $log, myTableOption, reqDataByAjax) {

        // TAB显示之后，调整表格大小
        $scope.$on("shown.bs.tab", function(event, element, target) {
            if (target.is('#permissionlist')) {
                $scope.$root.$broadcast('resize.table', '#permissiontable');
            }
        });

        $scope.option = myTableOption;
        $scope.option.ajax = ajax;

        function ajax(data, callback, settings) {
            reqDataByAjax("/scheduling/api/user/permission/getall", data).then(function (ret) {
                callback({
                    "draw": data.draw,
                    "recordsTotal": ret[1][0].recordsTotal,
                    "recordsFiltered": ret[2][0].recordsFiltered,
                    "data": ret[0]
                });
            });
        }

        // 记录当前选中权限
        $scope.$root.selectedPermissions = [];
        $scope.$on("select", function(event, element, e, dt, type, indexes) {
            if (element.is("#permissiontable"))
                $scope.$root.selectedPermissions = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
        });
        $scope.$on("deselect", function(event, element, e, dt, type, indexes) {
            if (element.is("#permissiontable"))
                $scope.$root.selectedPermissions = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
        });
        $scope.$on("draw.dt", function(event, element, e, settings) {
            if (element.is("#permissiontable"))
                $scope.$root.selectedPermissions = [];
        });
    });


    myApp.controller("addPermissionController", function($scope, $log, reqDataByAjax) {
        // 显示之前，初始化TAB内容
        $scope.$on("show.bs.tab", function(event, element, target) {
            if (target.is('#addpermission')) {
                $scope.permission = {
                    status: 1
                };
            }
        });

        // 保存角色信息
        $scope.save = function(e) {
            reqDataByAjax("/scheduling/api/user/permission/addpermission", $scope.permission, "POST")
                .then(function(data) {
                $scope.$root.$broadcast("refresh.table", "#permissiontable");
            });
        }
    });

    myApp.controller("modifyPermissionController", function($scope, $log, reqDataByAjax) {
        // 显示之前，初始化TAB内容
        $scope.$on("show.bs.tab", function(event, element, target) {
            if (target.is('#modifypermission')) {
                $scope.permission = $scope.$root.selectedPermissions[0];
            }
        });

        // 保存角色信息
        $scope.save = function(e) {
            reqDataByAjax("/scheduling/api/user/permission/modifypermission", $scope.permission, "POST")
                .then(function(data) {
                $scope.$root.$broadcast("refresh.table", "#permissiontable");
            });
        }
    });

    myApp.controller("deletePermissionController", function($scope, $log, reqDataByAjax) {
        $scope.ok = function() {
            reqDataByAjax("/scheduling/api/user/permission/deletepermission", $scope.$root.selectedPermissions, "POST")
            .then(function() {
                $scope.$root.$broadcast("refresh.table", "#permissiontable");
            });
        };
    });


    myApp.controller("myRoleController", function($scope, $log
        , myPermissionOption, reqDataByAjax
    ) {
        // 切换至权限表页面时，刷新权限表
        $scope.$on("shown.bs.tab", function(event, element, target) {
            if (target.is("#permissionroles"))
                $scope.$root.$broadcast("refresh.table", "#roletable");
        });

        // 设置用户表Option
        $scope.option = myPermissionOption;
        $scope.option.ajax = ajax;
        function ajax(data, callback, settings) {
            data.permission_id = $scope.$root.selectedPermissions && $scope.$root.selectedPermissions.length && $scope.$root.selectedPermissions[0].permission_id;
            reqDataByAjax("/scheduling/api/user/permission/getallrole", data).then(function (ret) {
                callback({
                    "draw": data.draw,
                    "recordsTotal": ret[1][0].recordsTotal,
                    "recordsFiltered": ret[2][0].recordsFiltered,
                    "data": ret[0]
                });
            });
        }

        // 设置角色表默认选中
        $scope.$on("draw.dt", function(event, element, e, settings) {
            if (!element.is('#roletable'))
                return;

            var table = $.fn.dataTable.tables({ visible: false, api: true }).table(element);
            if (!table.node())
                return;

            var si = [];
            table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
                var d = this.data();
                d.selected && si.push(rowIdx);
            });

            table.rows(si).select();
        });

        // 记录当前选中角色
        $scope.roles = [];
        $scope.$on("select", function (event, element, e, dt, type, indexes) {
            if (element.is("#roletable"))
                $scope.roles = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
        });

        $scope.$on("deselect", function (event, element, e, dt, type, indexes) {
            if (element.is("#roletable"))
                $scope.roles = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
        });

        $scope.$on("draw.dt", function (event, element, e, settings) {
            if (element.is("#roletable"))
                $scope.roles = [];
        });

        // 保存权限角色设置
        $scope.ok = function() {
            var permission_id = $scope.$root.selectedPermissions[0].permission_id;
            var roles   = $scope.roles;
            var url;
            if (roles.length == 0) {
                roles.push({ permission_id : permission_id });
                url = "/scheduling/api/user/permission/clearpermissionroles";
            } else {
                for (var p in roles)
                    roles[p].permission_id = permission_id;
                url = "/scheduling/api/user/permission/savepermissionroles";
            }
            reqDataByAjax(url, roles, "POST");
        };
    });

})();